<?php
class Rnaasv_Acl extends Zend_Acl  {
	
	private $_auth;

	public $resourcesArray = array();
	
    public function __construct(array $_resourcesArray) { 
    	$this->resourcesArray = $_resourcesArray;
    	
        $this
        	//roles
             ->addRole(new Zend_Acl_Role('general')) 
             ->addRole(new Zend_Acl_Role('con_sesion'), 'general') 
             ->addRole(new Zend_Acl_Role('adulto'), 'con_sesion') 
             ->addRole(new Zend_Acl_Role('institucional'), 'adulto') 
             ->addRole(new Zend_Acl_Role('grupo'), 'adulto') 
             ->addRole(new Zend_Acl_Role('institucional_grupo'), array('institucional', 'grupo')) 
             ->addRole(new Zend_Acl_Role('joven'), 'con_sesion') 
             ->addRole(new Zend_Acl_Role('lobato'), 'joven') 
             ->addRole(new Zend_Acl_Role('scout'), 'joven') 
             ->addRole(new Zend_Acl_Role('rover'), 'joven');

             $this->updateResources($_resourcesArray);
             // resources
             //->add("inscribirPatrullas") 
             //->add("modificarPatrullas", "inscribirPatrullas") 
             //rules
             //->allow(array('scout','grupo'),'inscribirPatrulla') 
             //->allow(array('scout','grupo'),'inscribirPatrulla') 
             //->allow(..) // rules 
        ;
    } 
    
    private function updateResources(array $_resources, Rnaasv_MenuResource $parent = null)    {
		for ($i = 0; $i < sizeof($_resources); $i++) {
             $rsc = $_resources[$i];
             if ($parent == null) {
	            $this->add($rsc);
             } else {
             	$this->add($rsc, $parent->getResourceId());
             }
             $this
             	->allow($rsc->getRolesAllowed(), $rsc)
             	->deny($rsc->getRolesDenyed(), $rsc);
             $this->updateResources($rsc->sub, $rsc);
        }
    	;
    }
     
 
    public function isAllowed($role = null, $resource = null, $privilege = null) { 
        $aclRole = $role; 
        $aclResource = $resource; 
         
        if ($role instanceof stdClass && !($role instanceof Rnaasv_User)) { 
            $aclRole = new Rnaasv_User($role); 
        } 
 
        //if ($resource instanceof MyModule_BlogPost) { 
        //    $aclResource = new MyModule_Acl_Resource_BlogPost($resource); 
        //} 
         
        return parent::isAllowed($aclRole, $aclResource, $privilege); 
     } 
     
    public function isUserInRole(Rnaasv_User $user, $role) {
    	return $user->getRoleId() == $role;
    }
} 


?>
